package tk.mybatis.web.controller;

import java.io.*;
import java.util.*;

import javax.websocket.*;
import javax.websocket.server.*;

import com.fasterxml.jackson.databind.ObjectMapper;

@ServerEndpoint("/ticTacToe/{gameId}/{username}")
public class TicTacToeServer {
	private static Map<Long, Game> games = new HashMap<>();
	private static ObjectMapper mapper = new ObjectMapper();

@OnOpen
	public void onOpen(Session session, @PathParam("gameId") long gameId, @PathParam("username") String username) {
//		try {
//			TicTacToeGame ticTAcToeGame = TicTacToeGame.getActiveGame(gameId);
//			if (ticTacToeGame != null) {
//				session.close(
//						new CloseReason(CloseReason.CloseCodes.UNEXPECTED_CONDITION, "This game has already started."));
//			}
//			List<String> actions = session.getRequestParameterMap().get("action");
//			if (actions != null && actions.size() == 1) {
//				String action = actions.get(0);
//				if ("start".equalsIgnoreCase(action)) {
//					Game game = new Game();
//					game.gameId = gameId;
//					game.player1 = session;
//					TicTacToeServer.games.put(gameId,game);
//				}else if ("join".equalsIgnoreCase(action))) {
//					Game game=TicTacToeServer.games.get(gameId);
//					game.player2=session;
//					game.ticTacToeGame=TicTacToeGame.startGame(gameId,username);
//					this.sendJsonMesssage(game.player1,game,new GameStartedMessage(game.ticTacToeGame));
//					this.sendJsonMessage(game.player2,game,new GameStartedMessage(game.ticTacToeGame));
//
//				}
//			}
//		} catch (IOException e) {
//			e.printStackTrace();
//			try {
//				session.close(new CloseReason(CloseReason.CloseCodes.UNEXPECTED_CONDITION,e.toString()));
//			} catch (IOException ignore) {
//				// TODO: handle exception
//			}
//		}
	}

	@OnMessage
	public void onMessage(Session session, String message, @PathParam("gameId") long gameId) {

	}
	
	private static class Game{
		public long gameId;
		public Session player1;
		public Session player2;
		//public TicTacToeGame ticTacToeGame;
	}
}